import time

from scrapy import signals


class TimeMonitorExtension(object):
    """
        监控爬虫开始、结束时间
    """
    def __init__(self, crawler):
        self.crawler = crawler
        self.start_time = None

    @classmethod
    def from_crawler(cls, crawler):
        ext = cls(crawler)
        crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened)
        crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed)
        return ext

    def spider_opened(self, spider):
        self.start_time = time.time()

    def spider_closed(self, spider):
        end_time = time.time()
        spider.logger.info(f"爬虫 {spider.name} 开始时间： {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.start_time))}")
        spider.logger.info(f"爬虫 {spider.name} 完成时间： {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))}")